package LeetcodeTest_one;
//罗马数字转整数
public class RomanToInt_one
{
    public static void main(String[] args)
    {
        int result=Solution.romanToInt("MMMCMXCIX");
        System.out.println(result);
    }
}
class Solution
{
    public static int romanToInt(String s)
    {
        if(s==null)
            return 0;
        String Roman="IVXLCDM";
        int []Number={1,5,10,50,100,500,1000};
        int sum,i,j;
        sum=i=0;
        j=s.length()-1;
        while(j>=0)
        {
            char digit=s.charAt(j);
            i=Roman.indexOf(digit);
            sum+=Number[i];
            if(j>=1 && Roman.indexOf( s.charAt(j-1))<i )
                sum -= Number[Roman.indexOf(s.charAt(--j))];
            j--;
        }
        if(sum>=1 && sum<=3999)
            return sum;
        else
            return 0;
    }
}